/**
Copyright (c) 2008 New Gonzo <john@newgonzo.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
**/


package com.newgonzo.cannonball.browser.actions
{
	import flash.events.EventDispatcher;
	
	import com.newgonzo.cannonball.browser.context.IContext;
	import com.newgonzo.cannonball.browser.actions.IAction;
	import com.newgonzo.cannonball.browser.events.ActionEvent;
	
	public class Action extends EventDispatcher implements IAction
	{
		private var actionType:String;
		
		private var isStarted:Boolean = false;
		private var isComplete:Boolean = false;
		private var isCancelled:Boolean = false;
		private var progressCache:Number = NaN;
		
		private var actionData:Object;
		private var actionContext:IContext;
		private var actionResult:Object;
		
		private var actionHandler:IActionHandler;
		
		public function Action(type:String, context:IContext, data:Object = null)
		{
			actionType = type;
			actionContext = context;
			actionData = data;
		}
		
		public function get type():String
		{
			return actionType;
		}
		
		public function get context():IContext
		{
			return actionContext;
		}
		
		public function get data():Object
		{
			return actionData;
		}
		
		public function set data(value:Object):void
		{
			actionData = value;
		}
		
		public function get complete():Boolean
		{
			return isComplete;
		}
		
		public function set complete(value:Boolean):void
		{
			if(isComplete)
			{
				return;
			}
			
			if(value)
			{
				isComplete = true;
				dispatchEvent(new ActionEvent(ActionEvent.COMPLETE, this, actionResult));
			}
		}
		
		public function get started():Boolean
		{
			return isStarted;
		}
		
		public function get progress():Number
		{
			return progressCache;
		}
		
		public function get handler():IActionHandler
		{
			return actionHandler;
		}
		
		public function set handler(value:IActionHandler):void
		{
			actionHandler = value;
		}
		
		public function execute():void
		{
			isStarted = true;
			
			actionHandler.execute(this);
			
			dispatchEvent(new ActionEvent(ActionEvent.START, this));
		}
		
		public function cancel():void
		{
			isCancelled = true;
			
			actionHandler.cancel(this);
			
			dispatchEvent(new ActionEvent(ActionEvent.CANCEL, this));
		}
		
		public function get cancelled():Boolean
		{
			return isCancelled;
		}
		
		public function get result():Object
		{
			return actionResult;
		}
		
		public function set result(value:Object):void
		{
			if(isComplete)
			{
				return;
			}

			actionResult = value;
			
			setProgress(1);
			dispatchEvent(new ActionEvent(ActionEvent.RESULT, this, value));
		}
		                                                        
		public function setProgress(value:Number):void
		{
			if(isComplete)
			{
				return;
			}
			
			var newVal:Number = (value < 0) ? 0 : (value > 1) ? 1 : value
			
			if(newVal != progressCache)
			{
				progressCache = newVal;
				dispatchEvent(new ActionEvent(ActionEvent.PROGRESS, this, newVal));
			}
		}
		
		override public function toString():String
		{
			return (isCancelled ? "Cancelled " : "") + "Action(progress=" + progressCache + ")";
		}
	}
}